package com.yunupmall.service.admin.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.CommiditySpecesVo;
import com.yunupmall.api.fordentity.SpecsCommodity;
import com.yunupmall.api.po.SpecsCommodityPo;
import com.yunupmall.mapper.commoditymapper.SpecsCommdityMapper;
import com.yunupmall.service.admin.service.SpecsCommdityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SpecsCommdityimpl extends ServiceImpl<SpecsCommdityMapper, SpecsCommodity> implements SpecsCommdityService {
    /**
     * 插入商品规格
     * @param commiditySpecesVo
     * @return
     */
    @Override
    @Transactional
    public String insertSpect(CommiditySpecesVo commiditySpecesVo) {
        if (ObjectUtil.isEmpty(commiditySpecesVo.getId())) {
            throw new RuntimeException("ID为空");
        }
        commiditySpecesVo.getSkuDataPo().forEach(sku->{
            if(ObjectUtil.isEmpty(sku.getSkuPrice())||ObjectUtil.isEmpty(sku.getSkuStock())||ObjectUtil.isEmpty(sku.getPropvalnames())){
                throw new RuntimeException("商品库存、商品价格或商品规格为空");
            }
        });
        List<SpecsCommodity> specsCommoditieList = new ArrayList<>();
        final SpecsCommodity[] specsCommodity = new SpecsCommodity[1];
        commiditySpecesVo.getSkuDataPo().forEach(sku -> {
            specsCommodity[0] = new SpecsCommodity();
            specsCommodity[0].setCSku(sku.getPropvalnames());
            specsCommodity[0].setCId(commiditySpecesVo.getId());
            specsCommodity[0].setPrices(sku.getSkuPrice());
            specsCommodity[0].setStocks(sku.getSkuStock());
            specsCommodity[0].setCreateTime(LocalDateTime.now());
            specsCommodity[0].setDeflag(0);
            specsCommoditieList.add(specsCommodity[0]);
        });
        if (this.saveBatch(specsCommoditieList)) {
            return "新增成功";
        }else {
            throw new RuntimeException("新增失败");
        }
    }

    /**
     * 获取商品规格page
     * @param cId
     * @return
     */
    @Override
    public Page<SpecsCommodity> getList(Integer cId,Integer pageID,Integer limit ) {
        if (ObjectUtil.isEmpty(cId)) {
            throw new RuntimeException("商品ID为空");
        }
        Page<SpecsCommodity> specsCommodityPage = new Page<>(pageID, limit);
        LambdaQueryWrapper<SpecsCommodity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpecsCommodity::getCId,cId);
        wrapper.eq(SpecsCommodity ::getDeflag,0);
        Page<SpecsCommodity> page = this.page(specsCommodityPage, wrapper);
        return page;
    }

    /**
     * 获得商品规格信息
     * @param specsCommodityPo
     * @return
     */
    @Override
    public List<SpecsCommodity> getListData(SpecsCommodityPo specsCommodityPo) {
        LambdaQueryWrapper<SpecsCommodity> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(specsCommodityPo.getCId())){
            wrapper.eq(SpecsCommodity ::getCId,specsCommodityPo.getCId());
        }
        if (ObjectUtil.isNotNull(specsCommodityPo.getId())) {
            wrapper.eq(SpecsCommodity ::getId,specsCommodityPo.getId());
        }
        if (ObjectUtil.isNotEmpty(specsCommodityPo.getIdList())){
            wrapper.in(SpecsCommodity ::getId,specsCommodityPo.getIdList());
        }
        wrapper.eq(SpecsCommodity ::getDeflag,0);
       return this.list(wrapper);
    }

    /**
     * 删除商品规格
     * @param id
     * @return
     */
    @Override
    @Transactional
    public String del(Integer id) {
        if (ObjectUtil.isEmpty(id)){
            throw new RuntimeException("id为空");
        }
        SpecsCommodity specsCommodity = new SpecsCommodity();
        specsCommodity.setId(id);
        specsCommodity.setDeflag(1);
        if (this.updateById(specsCommodity)){
         return "删除成功";
        }else {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public SpecsCommodity byid(Integer id) {
        LambdaQueryWrapper<SpecsCommodity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpecsCommodity ::getId,id);
        wrapper.eq(SpecsCommodity ::getDeflag,0);
        List<SpecsCommodity> list = this.list(wrapper);
        if (list.size() > 0) {
            return list.get(0);
        }else {
            return null;
        }
    }

}
